using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace dataplotcli {
    public partial class PlotUI : Form {
        GridLogic grid_backend;


        public PlotUI() {
            InitializeComponent();
            grid_backend = new GridLogic(25, 25, grid_panel.Width, grid_panel.Height);
            grid_backend.blank_grid(Brushes.DarkGray);
            grid_backend.draw_grid_lines(Brushes.LightYellow);
        }

        private void grid_panel_MouseClick(object sender, MouseEventArgs e) {
            toolStripStatusLabel1.Text = grid_backend.select_cell(e.X, e.Y);
            redraw_grid_panel();
        }

        private void grid_panel_double_click_add_new_photo(object sender, MouseEventArgs e) {
            try {
                string[] image_names;
                Stream file_stream;
                openFileDialog1.Filter = 
                    "JPEG Images (*.jpg,*.jpeg)|*.jpg|Gif Images" +
                    "(*.gif)|*.gif|Bitmaps (*.bmp)|*.bmp|PNG Images (*.png)|*.png";
                openFileDialog1.FilterIndex = 1;
                if (openFileDialog1.ShowDialog() == DialogResult.OK) {
                    if ((file_stream = openFileDialog1.OpenFile()) != null) {
                        image_names = openFileDialog1.FileNames;
                        file_stream.Close();
                        foreach(string image_name in image_names) {
                            grid_panel.CreateGraphics().DrawImage(
                                grid_backend.insert_photo_into_grid(
                                    e.X, e.Y, new PlotablePhoto(image_name)),0,0);
                        }
                        grid_backend.draw_cell(e.X, e.Y);
                        redraw_grid_panel();
                    }
                }
            } catch (FileNotFoundException file_not_found_e) {
                Console.WriteLine("File " + file_not_found_e.FileName + " not found or permission denied.");
            }
            // GOOD
        }

        private void grid_panel_MouseMove(object sender, MouseEventArgs e) {

            /*if (grid_backend.get_cell_photo_count(e.X, e.Y) > 0 //&& 
                //grid_backend.preview_mode &&
                //grid_backend.hovering(e.X,e.Y)
                )
            {
                this.preview_cell(grid_backend.get_cell_preview(e.X, e.Y));
            }*/
            // Commented out for the interest of better performance.
        }

        private void grid_panel_Paint(object sender, PaintEventArgs e) {
            if (null != grid_backend.SelectedDrawPanelState) {
                grid_panel.CreateGraphics().DrawImage(grid_backend.SelectedDrawPanelState, 0, 0);
            } else {
                grid_panel.CreateGraphics().DrawImage(grid_backend.DrawPanelState, 0, 0);
            }
        }

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
            grid_backend.select_photo(new PlotablePhoto(e.Item.Text));

            pictureBox1.Image = ((PlotablePhoto)grid_backend.CurrentSelectedPhoto).PlotedImage;
            textBox1.Text = grid_backend.CurrentSelectedPhoto.Name;

            if (pictureBox1.SizeMode != PictureBoxSizeMode.Zoom) {
                panel1.AutoScroll = true;
                pictureBox1.Size = pictureBox1.Image.Size;
            } else {
                panel1.AutoScroll = false;
                pictureBox1.Size = panel1.Size;
            }
        }

        private void rotate_zoom_view_Click(object sender, EventArgs e) {
            if(null!= pictureBox1.Image) {
                pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                pictureBox1.Refresh();
            }
        }

    


        private void preview_cell(int total_to_preview) {
            imageList1.Images.Clear();
            listView1.Clear();
            for (int i=0;i<total_to_preview;i++) {
                PlotablePhoto photo = (PlotablePhoto)grid_backend.NextOrPreviousEntryInCurrentSelectedCell;
                imageList1.Images.Add(photo.PlotedImageSmall);
                imageList1.ImageSize = new Size(75, 75);
                listView1.Items.Add(photo.Name, i);
            }
            listView1.LargeImageList = imageList1;
        }

        private void toolStripButton1_Click(object sender, EventArgs e) {
           grid_backend.reset_current_selected_cell_list_index_location();
           this.preview_cell(grid_backend.number_of_entries_to_load_at_a_time);
        }

        private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e) {
            if (pictureBox1.SizeMode == PictureBoxSizeMode.Zoom) {
                pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
                pictureBox1.Size = pictureBox1.Image.Size;
                panel1.AutoScroll = true;
            } else {
                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                pictureBox1.Size = panel1.Size;
                panel1.AutoScroll = false;
            }
        }


        private void redraw_grid_panel() {
            grid_panel.CreateGraphics().DrawImage(grid_backend.SelectedDrawPanelState, 0, 0, grid_panel.Width, grid_panel.Height);
        }

        private void picture_zoom_Click(object sender, EventArgs e) {
            if (pictureBox1.SizeMode == PictureBoxSizeMode.Zoom) {
                pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
                pictureBox1.Size = pictureBox1.Image.Size;
                panel1.AutoScroll = true;
            } else {
                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                pictureBox1.Size = panel1.Size;
                panel1.AutoScroll = false;
            }
        }

        private void picture_rotate_Click(object sender, EventArgs e) {
            if (null != pictureBox1.Image) {
                pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                pictureBox1.Refresh(); // GOOD
            }
        }

        private void preview_next_Click(object sender, EventArgs e) {
            grid_backend.current_cell_list_go_forward();
            this.preview_cell(grid_backend.number_of_entries_to_load_at_a_time);
        }

        private void preview_previous_Click(object sender, EventArgs e) {
            grid_backend.current_cell_list_go_backward();
            this.preview_cell(grid_backend.number_of_entries_to_load_at_a_time);
        }

        private void picture_next_Click(object sender, EventArgs e) {
            grid_backend.current_cell_list_go_forward();
            pictureBox1.Image = ((PlotablePhoto)grid_backend.NextOrPreviousEntryInCurrentSelectedCell).PlotedImage;
            textBox1.Text = ((PlotablePhoto)grid_backend.NextOrPreviousEntryInCurrentSelectedCell).Name;

        }

        private void picture_previous_Click(object sender, EventArgs e) {
            grid_backend.current_cell_list_go_backward();
            pictureBox1.Image = ((PlotablePhoto)grid_backend.NextOrPreviousEntryInCurrentSelectedCell).PlotedImage;
            textBox1.Text = ((PlotablePhoto)grid_backend.NextOrPreviousEntryInCurrentSelectedCell).Name;
        }

    }
}